package com.onlinevideo.servlet;

import cn.hutool.json.JSONUtil;
import com.onlinevideo.data.ResultBean;
import com.onlinevideo.entity.Category;
import com.onlinevideo.entity.Role;
import com.onlinevideo.service.CategoryService;
import com.onlinevideo.service.RoleService;
import com.onlinevideo.service.impl.CategoryServiceImpl;
import com.onlinevideo.service.impl.RoleServiceImpl;
import com.onlinevideo.util.MyBeanUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class CategoryServlet  extends BaseServlet{

    private PrintWriter writer = null;
    private CategoryService categoryService = new CategoryServiceImpl();
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException {
        writer =resp.getWriter();
        Category category = MyBeanUtils.set(Category.class, req.getParameterMap());
        if(category.getCategoryName()==null||category.getCategoryName().length()<=0){
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationError("分类新增失败，未输入分类名称或输入的长度为0")));
            return;
        }
        Category category1 = categoryService.queryByCategory(category.getCategoryName());
        if(category1!=null){
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationError(category.getCategoryName()+"新增失败，数据库已经存在该分类")));
            return;
        }
        int result = categoryService.insert(category);
        if(result>0){
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess(category.getCategoryName()+"新增成功")));
        }else {
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationError(category.getCategoryName()+"新增失败")));
        }
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException {
        writer =resp.getWriter();
        Category category = MyBeanUtils.set(Category.class, req.getParameterMap());
        if(category.getCategoryName()==null||category.getCategoryName().length()<=0){
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationError("分类修改失败，未输入分类名称或输入的长度为0")));
            return;
        }
        Category category1 = categoryService.queryByCategory(category.getCategoryName());
        if(category1!=null){
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationError(category.getCategoryName()+"修改失败，数据库已经存在该分类")));
            return;
        }
        int result = categoryService.update(category);
        if(result>0){
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess(category.getCategoryName()+"修改成功")));
        }else {
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationError(category.getCategoryName()+"修改失败")));
        }
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException {
        writer =resp.getWriter();
        Category category = MyBeanUtils.set(Category.class, req.getParameterMap());


        int result = categoryService.delete(category.getId());
        if(result>0){
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess("编号:"+category.getId()+"删除成功")));
        }else {
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationError("编号:"+category.getId()+"删除失败")));
        }
    }
}
